package cn.iocoder.yudao.module.infrastructure.messy.controller.admin;

import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.infrastructure.messy.vo.RedisMonitorRespVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.data.redis.connection.RedisServerCommands;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Properties;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - Redis 监控")
@RestController
@RequestMapping("/infra/redis")
public class RedisController {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("/get-monitor-info")
    @Operation(summary = "获得 Redis 监控信息")
    @PreAuthorize("@ss.hasPermission('infra:redis:get-monitor-info')")
    public CommonResult<RedisMonitorRespVO> getRedisMonitorInfo() {
        // 获得 Redis 统计信息
        Properties info = stringRedisTemplate.execute((RedisCallback<Properties>) RedisServerCommands::info);
        Long dbSize = stringRedisTemplate.execute(RedisServerCommands::dbSize);
        Properties commandStats = stringRedisTemplate.execute((
                RedisCallback<Properties>) connection -> connection.serverCommands().info("commandstats"));
        assert commandStats != null; // 断言，避免警告
        // 拼接结果返回
        return success(build(info, dbSize, commandStats));
    }

    private RedisMonitorRespVO build(Properties info, Long dbSize, Properties commandStats) {
        RedisMonitorRespVO respVO = new RedisMonitorRespVO();
        respVO.setInfo(info);
        respVO.setDbSize(dbSize);
        respVO.setCommandStats(new ArrayList<>(commandStats.size()));
        commandStats.forEach((key, value) -> {
            RedisMonitorRespVO.CommandStat commandStat = new RedisMonitorRespVO.CommandStat();
            commandStat.setCommand(StrUtil.subAfter((String) key, "cmdstat_", false));
            commandStat.setCalls(Long.valueOf(StrUtil.subBetween((String) value, "calls=", ",")));
            commandStat.setUsec(Long.valueOf(StrUtil.subBetween((String) value, "usec=", ",")));
            respVO.getCommandStats().add(commandStat);
        });
        return respVO;
    }

}
